package com.example.xxcjgl.ui.components.score

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import com.example.xxcjgl.ui.components.common.InlineErrorMessage

@Composable
fun ScoreInput(
    label: String,
    value: String,
    onValueChange: (String) -> Unit,
    isEncrypted: Boolean = false,
    modifier: Modifier = Modifier
) {
    OutlinedTextField(
        value = if (isEncrypted) "****" else value,
        onValueChange = { if (!isEncrypted) onValueChange(it) },
        label = { Text(label) },
        modifier = modifier.fillMaxWidth(),
        enabled = !isEncrypted,
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
        singleLine = true,
        trailingIcon = {
            if (isEncrypted) {
                Icon(Icons.Default.Lock, contentDescription = "已加密")
            }
        }
    )
}

@Composable
fun ValidatedScoreInput(
    label: String,
    value: String,
    onValueChange: (String) -> Unit,
    isValid: Boolean = true,
    errorMessage: String = "请输入有效的成绩",
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier) {
        OutlinedTextField(
            value = value,
            onValueChange = onValueChange,
            label = { Text(label) },
            modifier = Modifier.fillMaxWidth(),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
            singleLine = true,
            isError = !isValid
        )

        if (!isValid) {
            InlineErrorMessage(message = errorMessage)
        }
    }
}